﻿using PersistentObject.Common.Schema;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PersistentObject.Common.Criteria
{
    /// <summary>
    /// Represent an EXISTS clause
    /// </summary>
    public class ExistsCriteria : BaseCriteria
    {
        /// <summary>
        /// Indicate the table in the EXISTS clause
        /// </summary>
        public Type InnerType { get; private set; }
        /// <summary>
        /// Indicate the criteria in the EXISTS clause
        /// </summary>
        public BaseCriteria InnerCriteria { get; private set; }
        /// <summary>
        /// Indicate the links between the inner tables and the outer tables
        /// </summary>
        public ExistsLink[] Links { get; private set; }

        /// <summary>
        /// The constructor of EXISTS criteria
        /// </summary>
        /// <param name="type">Indicate the mapping type in the EXISTS clause</param>
        /// <param name="links">Indicate the links between criteria-inner types and criteria-outer types</param>
        /// <param name="criteria">Indicate the criteria in the EXISTS clause</param>
        public ExistsCriteria(Type innerType, BaseCriteria innerCriteria, ExistsLink[] links)
        {
            InnerType = innerType;// SchemaHelper.CreateInnerTable(innerType);
            InnerCriteria = innerCriteria;
            Links = links;
        }

        //public ExistsCriteria(Type innerType, BaseCriteria innerCriteria, Table mainTable, Tuple<Column, string>[] links)
        //{
        //    MainTable = mainTable;
        //    InnerTable = SchemaHelper.CreateInnerTable(innerType);
        //    InnerCriteria = innerCriteria;

        //    Links = new Tuple<Column, Column>[links.Count()];
        //    for (int i = 0; i < links.Count(); i++)
        //    {
        //        var innerColumn = InnerTable.GetMappingColumn(links[i].Item2);
        //        Links[i] = new Tuple<Column, Column>(links[i].Item1, innerColumn);
        //    }
        //}

        //public ExistsCriteria(Type innerType, BaseCriteria innerCriteria, Type mainType, Tuple<string, string>[] links)
        //{
        //    Table mainTable = SchemaHelper.CreateTable(mainType);
        //    InnerTable = SchemaHelper.CreateInnerTable(innerType);
        //    InnerCriteria = innerCriteria;

        //    Links = new Tuple<Column, Column>[links.Count()];
        //    for (int i = 0; i < links.Count(); i++)
        //    {
        //        var mainColumn = mainTable.GetMappingColumn(links[i].Item1);
        //        var innerColumn = InnerTable.GetMappingColumn(links[i].Item2);
        //        Links[i] = new Tuple<Column, Column>(mainColumn, innerColumn);
        //    }
        //}

        //public ExistsCriteria(Type innerType, BaseCriteria innerCriteria, Table mainTable, Tuple<string, string>[] links)
        //{
        //    MainTable = mainTable;
        //    InnerTable = SchemaHelper.CreateInnerTable(innerType);
        //    InnerCriteria = innerCriteria;

        //    Links = new Tuple<Column, Column>[links.Count()];
        //    for (int i = 0; i < links.Count(); i++)
        //    {
        //        var mainColumn = mainTable.GetMappingColumn(links[i].Item1);
        //        var innerColumn = InnerTable.GetMappingColumn(links[i].Item2);
        //        Links[i] = new Tuple<Column, Column>(mainColumn, innerColumn);
        //    }
        //}
    }

    public class ExistsLink
    {
        public string MainTableProperty { get; set; }
        public string InnerTableProperty { get; set; }

        public ExistsLink(string mainTableProperty, string innerTableProperty)
        {
            MainTableProperty = mainTableProperty;
            InnerTableProperty = innerTableProperty;
        }

        public ExistsLink(Tuple<string, string> tuple)
        {
            MainTableProperty = tuple.Item1;
            InnerTableProperty = tuple.Item2;
        }
    }
}
